﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Tintuc
/// </summary>
public class Tintuc
{
	public Tintuc()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    DataConnection data = new DataConnection();
    public DataTable LayTT()
    {
        string sql = "select * from TinTuc";
        return data.GetData(sql);
    }
    public DataTable CTTinTuc(string ma)
    {
        string sql = "select * from TinTuc Where MaTT=@MaTT";
        SqlParameter p = new SqlParameter("@MaTT", ma);
        return data.GetData(sql, p);
    }

    public DataTable TimTT(string tieude,string tu,string den)
    {

        string sql = "select * from TinTuc where 1=1";
        if (tieude != null)
            sql = sql + " and TieuDe like '%'+@td+'%'";
        if (tu != null && den != null)
            sql = sql + " or NgayDang between @tu and @den";
        SqlParameter p = new SqlParameter("@td", tieude);
        SqlParameter p1 = new SqlParameter("@tu", tu);
        SqlParameter p2 = new SqlParameter("@den", den);
        return data.GetData(sql, p,p1,p2);

       
    }
    public DataTable ThemTT(string tieude, string tomtat, string ngaydang,string noidung, string tacgia,  string hinhanh)
    {
        SqlParameter p = new SqlParameter("@tieude", tieude);
        SqlParameter p1 = new SqlParameter("@tomtat", tomtat);
        SqlParameter p3 = new SqlParameter("@ngaydang", ngaydang);
        SqlParameter p2 = new SqlParameter("@noidung", noidung);
        SqlParameter p5 = new SqlParameter("@tacgia", tacgia);
        SqlParameter p4 = new SqlParameter("@hinhanh", hinhanh);

        string sql = "insert into TinTuc values(@tieude,@tomtat,@ngaydang,@noidung,@tacgia,@hinhanh)";
        return data.GetData(sql,p,p1,p3,p2,p5,p4);
    }
    public DataTable XoaTT(int ma)
    {
        SqlParameter p = new SqlParameter("@ma", ma);
        string sql = "delete from TinTuc where MaTT=@ma";
        return data.GetData(sql, p);
    }
    public DataTable SuaTT(int ma,string tieude, string tomtat, string ngaydang,string noidung, string tacgia, string hinhanh)
    {
        SqlParameter p = new SqlParameter("@tieude", tieude);
        SqlParameter p1 = new SqlParameter("@tomtat", tomtat);
        SqlParameter p3 = new SqlParameter("@ngaydang", ngaydang);
        SqlParameter p4 = new SqlParameter("@hinhanh", hinhanh);
        SqlParameter p5 = new SqlParameter("@tacgia", tacgia);
        SqlParameter p6 = new SqlParameter("@ma", ma);
        SqlParameter p7 = new SqlParameter("@noidung", noidung);
        string sql = "update TinTuc  set TieuDe=@tieude,TomTat=@tomtat,NgayDang=@ngaydang,NoiDung=@noidung,TacGia=@tacgia,HinhAnh=@hinhanh where MaTT=@ma";
        return data.GetData(sql, p6, p, p1, p3, p4, p5,p7);
    }
}
